package com.example.travel.Controller;

import com.example.travel.Service.UserServer;
import com.example.travel.domain.Users;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 功能：
 * 作者：lln
 * 日期：2025/3/17 12:48
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserServer userServer;


    //http://localhost:8080/users/register?username=lln&password=123456&email=123456@qq.com&avatar=https://avatars.githubusercontent.com/u/102040355?v=4
    //注册功能
    @GetMapping("/register")
    public boolean register(@RequestParam String username, @RequestParam String password, @RequestParam String email,@RequestParam String avatar) {
        Users user = new Users();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setAvatar(avatar);
        user.setRegistrationdate(new java.util.Date());
        return userServer.register(user);
    }

    //登录功能
    @GetMapping("/login")
    public Users login(HttpSession session, @RequestParam String username, @RequestParam String password, HttpServletResponse response) {
        Users users = userServer.seluser(username, password);
        if (users != null) {
            session.setAttribute("user", users.getUsername());
            session.setAttribute("userid", users.getUserid());
            Cookie cookie = new Cookie("JSESSIONID", session.getId());
            cookie.setPath("/");
            cookie.setMaxAge(60 * 60 * 24 * 7); // 设置 Cookie 有效期
            response.addCookie(cookie);
            //打印结果
            System.out.println(session.getAttribute("user"));
            System.out.println("登录成功");
            return users;
        } else {
            return null;
        }
    }
    //http://localhost:8080/users/getUsers?userids=1,2
    @GetMapping("/getUsers")
    public List<Users> getUsers(@RequestParam String userids) {
        int[] ids = Arrays.stream(userids.split(","))
                .mapToInt(Integer::parseInt)
                .toArray();
        return userServer.seluserbyid(ids);
    }
    //添加好友
    //http://localhost:8080/users/addFriend/5?friendid=1
    @GetMapping("/addFriend/{userid}")
    public ResponseEntity<?> addFriend(HttpSession session, @PathVariable int userid, @RequestParam String friendid) {
        // 调用服务层方法添加好友
        boolean result = userServer.addFriend(userid, Integer.parseInt(friendid));
        return ResponseEntity.ok(result);
    }
    //删除好友
    //http://localhost:8080/users/removeFriend/5?friendid=1
    @GetMapping("/removeFriend/{userid}")
    public ResponseEntity<?> removeFriend(HttpSession session, @PathVariable int userid, @RequestParam String friendid) {
        // 调用服务层方法删除好友
        boolean result = userServer.removeFriend(userid, Integer.parseInt(friendid));
        return ResponseEntity.ok(result);
    }
}
